#pragma once
#include<assert.h>


namespace bite
{
	class string
	{
	private:
		char* _str;
		size_t _size;
		size_t _capacity;

		//const static int npos = -1;
	public:
		//typedef char* iterator;

		typedef char* iterator;

		/*iterator begin()
		{
			return _str;
		}*/

		iterator begin()
		{
			return _str;
		}



		iterator end()
		{
			return _str + _size;
		}

		/*string(const char* str = "")
			:_size(strlen(str))
			,_capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}*/

		string(const char* str = "")
			:_size(strlen(str))
			, _capacity(_size)
		{
			_str = new char[_capacity + 1];
			strcpy(_str, str);
		}


		/*char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}*/

		char& operator[](size_t pos)
		{
			assert(pos < _size);
			return _str[pos];
		}

		
		const char& operator[](size_t pos) const
		{
			assert(pos < _size);
			return _str[pos];
		}


		size_t capacity() const
		{
			return _capacity;
		}

		size_t size() const
		{
			return _size;
		}


		~string()
		{
			delete[] _str;
			_size = _capacity = 0;
		}

		char* c_str()
		{
			return _str;
		}

		/*void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];
				strcpy(temp,_str);
				delete[] _str;
				_str = temp;

				_capacity = n;
			}
		}*/

		/*void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];
				strcpy(temp, _str);
				delete[] _str;
				_str = temp;

				_capacity = n;
			}
		}*/

		void reserve(size_t n)
		{
			if (n > _capacity)
			{
				char* temp = new char[n + 1];
				strcpy(temp,_str);
				delete[] _str;
				_str = temp;

				_capacity = n;
			}
		}

		/*void pushback(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}*/

		void pushback(char ch)
		{
			if (_size == _capacity)
			{
				reserve(_capacity == 0 ? 4 : _capacity * 2);
			}
			_str[_size] = ch;
			++_size;
			_str[_size] = '\0';
		}

		/*void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}*/

		void append(const char* str)
		{
			size_t len = strlen(str);
			if (_size + len > _capacity)
			{
				reserve(_size + len);
			}
			strcpy(_str + _size, str);
			_size += len;
		}


		/*string& operator+=(char ch)
		{
			pushback(ch);
			return *this;
		}*/

		string& operator+=(char ch)
		{
			pushback(ch);
			return *this;
		}

		

		string& operator+=(const char* str)
		{
			append(str);
			return *this;
		}
	};

	void test1()
	{
		string str1("hello world");
		cout << str1.c_str() << endl;


		for (size_t i = 0; i < str1.size(); i++)
		{
			cout << str1[i] << " ";
		}
		cout << endl;

		/*for (size_t i = 0; i < str1.size(); i++)
		{
			cout << str1[i] << " ";
		}
		cout << endl;*/

		string::iterator it = str1.begin();
		while (it != str1.end())
		{
			(*it)++;
			cout << *it << " ";
			++it;
		}
		cout << endl;

		/*string::iterator it = str1.begin();
		while (it != str1.end())
		{
			(*it)++;
			cout << *it << " ";
			it++;
		}*/


		cout << str1.c_str() << endl;


		cout << "~~~~~~~~~~~~~~~~~~~~~~~~" << endl;


		for (auto ch : str1)
		{
			ch++;
			cout << ch << " ";
		}
		cout << endl;

		cout << str1.c_str() << endl;
	}



	void test2()
	{
		string str2;
		str2 += '  ';
		str2 += "hello world";
		cout << str2.c_str() << endl;
	}
}
